package com.android.fragmentdemo.definenestedscroll;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingChild;
import androidx.core.view.NestedScrollingChildHelper;
import androidx.core.view.ViewCompat;
import com.android.fragmentdemo.defineviews.NestedScrollRecyclerView;

public class TaNestedScrollChild extends LinearLayout implements NestedScrollingChild {
    private NestedScrollingChildHelper mChildHelper;
//  private NestedScrollRecyclerView mScrollRv;
    private final int[] offset = new int[2];
    private final int[] consumed = new int[2];
    private int downY = 0;
    private int showHeight = 0;

    public TaNestedScrollChild(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
//        mScrollRv = (NestedScrollRecyclerView) getChildAt(0);
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (mScrollRv.positionInDownOrUp()) {
//            return true;
//        } else {
//            return super.onInterceptTouchEvent(ev);
//        }
//    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //第一次测量，因为布局文件中高度是wrap_content，因此测量模式为almost，即高度不超过父控件的剩余空间
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        showHeight = getMeasuredHeight();

        //第二次测量，对高度没有任何限制，那么测量出来的就是完全展示内容所需要的高度
        //heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        performClick();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downY = (int) event.getRawY();
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                int moveY = (int) event.getRawY();
                int dy = moveY - downY;
                downY = moveY;
                if (startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL) && dispatchNestedPreScroll(0, dy, consumed, offset)) {
                    //如果找到了支持嵌套滑动的父类,父类进行了一系列的滑动
                    int remain = dy - consumed[1];
                    if (remain != 0) {
                        scrollBy(0, -remain);
                    }
                } else {
                    scrollBy(0, -dy);
                }
                break;
            }
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public void scrollTo(int x, int y) {
        //限制滚动范围
        int maxY = getMeasuredHeight() - showHeight;
        y = Math.max(0, Math.min(y, maxY));
        super.scrollTo(x, y);
    }

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        getScrollChildHelper().setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return getScrollChildHelper().isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return getScrollChildHelper().startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        getScrollChildHelper().stopNestedScroll();
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow) {
        return getScrollChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow) {
        return getScrollChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return getScrollChildHelper().dispatchNestedPreFling(velocityX, velocityY);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return getScrollChildHelper().dispatchNestedFling(velocityX, velocityY, consumed);
    }

//    public NestedScrollRecyclerView getScrollRv() {
//        return mScrollRv;
//    }

    private NestedScrollingChildHelper getScrollChildHelper() {
        if (mChildHelper == null) {
            mChildHelper = new NestedScrollingChildHelper(this);
            mChildHelper.setNestedScrollingEnabled(true);
        }
        return mChildHelper;
    }
}
